var Detali = {};
var detailObj = {
  jump() {
    $(document).ready(function () {
      // 当点击.back 元素时，执行 history.back() 回退操作
      $(".back").click(function () {
        history.back();
      });
      // 当点击 #index 元素时，跳转到 index.html
      $("#index").click(function () {
        location.href = "./index.html";
      });
    });
  },
  getData() {
    var webUrl = location.href;
    var index = webUrl.indexOf("=");
    var id = webUrl.slice(index + 1);
    var url = "http://localhost:3000/positionDataList/" + id;
    axios.get(url).then(function (res) {
      Detali = res.data;
      detailObj.render(res.data);
    });
  },
  render(obj) {
    // 渲染职位标题信息
    $("#job-title").html(`
         <div class="title lh15">
          <p class="f20">${obj.workTitle}</p>
          <p class="f18 fw-700 sc"><span>${obj.price}</span></p>
        </div>
        <div class="content bg-fff f14 c666 flex pb-15 pt-15 bb3">
          <p><i class="iconfont icon-ditu"></i><span>${obj.conditionAddress}</span></p>
          <p class="ml-10">
            <i class="iconfont icon-suoshuzhiwei"></i><span>${obj.conditionExperience}</span>
          </p>
          <p class="ml-10">
            <i class="iconfont icon-edu-line"></i><span>${obj.conditionEdu}</span
            ><span class="ml-25">更新：9分钟前</span>
          </p>
        </div>
        `);
    // 渲染人员信息
    $(".person").html(` <img
    src="${obj.imgUrl}"
    alt=""
  />
  <p class="flex fdc ml-10">
    <span>${obj.contactName}</span><span class="f12 c888 mt-10">${obj.companyName}</span>
  </p>`);
    // 渲染职位福利信息
    $("#welfare").html(` <p class="f18"><span>职位福利</span></p>
  <p class="sc f12 mt-10 bb3 pb-20"><span>${obj.welfare}</span> <span class="ml-5"> ${obj.welfare2}</span> <span class="ml-5"> ${obj.welfare3}</span></p>`);
    // 渲染职位详情信息
    $(".detail").html(`
     <p class="f18"><span>职位详情:</span></p>
    <p class="f14 text">${obj.jobDetails}
    </p>
    `);
  },
  apply() {
    $("#apply").click(function () {
      // 先判断数据是否存在
      var url = "http://localhost:3000/apply?positionId=" + Detali.id;
      axios.get(url).then(function (res) {
        if (res.data.length > 0) {
          // 修改按钮文本为已申请
          $("#apply1").hide();
          $("#apply2").show();
          alert("您已申请， 请不要重复申请");
          
        } else {
          var data = {
            imgUrl: Detali.imgUrl,
            workTitle: Detali.workTitle,
            price: Detali.price,
            time: Detali.time,
            positionId: Detali.id,
            userId: localStorage.userId,
            companyName: Detali.companyName,
            jobDetails: Detali.jobDetails,
            conditionAddress: Detali.conditionAddress,
            conditionExperience: Detali.conditionExperience,
            conditionEdu: Detali.conditionEdu,
          };
          var url = "http://localhost:3000/apply";
          axios.post(url,data).then(function (res) {
            location.href = "my-application.html";
            // 修改按钮文本为已申请
            $("#apply1").show();
            $("#apply2").hide();
            alert("申请成功"); 
          });
        }
      });
    });
  }
};


detailObj.jump();
detailObj.apply();
detailObj.getData();